package org.lanqiao.oa.service.impl;

import org.lanqiao.oa.composite.PermissionNode;
import org.lanqiao.oa.dao.PermissionDao;
import org.lanqiao.oa.dao.RolePermissionRelationDao;
import org.lanqiao.oa.dto.PermissionDTO;
import org.lanqiao.oa.mapper.ResultInfo;
import org.lanqiao.oa.service.PermissionService;
import org.lanqiao.oa.utils.BeanCopierUtil;
import org.lanqiao.oa.visitor.impl.CheckRolePermissionRelationVisitor;
import org.lanqiao.oa.visitor.impl.DeletePermissionVisitor;
import org.lanqiao.oa.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionRelationDao relationDao;

    /**
     * 获取权限
     * @return
     */
    @Override
    public List<PermissionVo> getPermission() {
        //从数据库查询出权限
        List<PermissionDTO> permissionDtos = permissionDao.selectList();
        ArrayList<PermissionVo> permissionVos = new ArrayList<>();
        //通过克隆只把需要的数据发送给页面
        for (PermissionDTO permissionDto : permissionDtos) {

            PermissionVo permissionVo = new PermissionVo();

            BeanCopierUtil.copy(permissionDto,permissionVo);
            permissionVos.add(permissionVo);
        }

        return permissionVos;
    }

    /**
     * 查询权限
     * @param id
     * @return
     */
    @Override
    public PermissionDTO selectById(String id) {
        return permissionDao.selectById(id);
    }

    /**
     * 添加权限
     * @param permissionDto
     * @return
     */
    @Override
    public boolean addPermission(PermissionDTO permissionDto) {
        permissionDto.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        Integer integer = permissionDao.addPermission(permissionDto);
        return integer>=1;
    }

    /**
     * 修改权限
     * @param permissionDto
     * @return
     */
    @Override
    public boolean updatePermission(PermissionDTO permissionDto) {
        return permissionDao.updatePermission(permissionDto)>=1;
    }


    /**
     * 判断权限是否可用
     * @param code
     * @return
     */
    @Override
    public ResultInfo ifAvailable(String code) {
        if(code==null || code==""){
            return ResultInfo.getResultInfo(false,"代码不能为空");
        }
        PermissionDTO permissionDtoByCode = permissionDao.getPermissionDtoByCode(code);
        if(permissionDtoByCode==null){
            return ResultInfo.getResultInfo(true,"代码可用");
        }else {
            return ResultInfo.getResultInfo(false,"代码重复");
        }
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @Override
    public boolean deletePermissionById(String id) {
        //根据id查询权限
        PermissionDTO permissionDTO = permissionDao.selectById(id);
        PermissionNode copy = permissionDTO.copy(PermissionNode.class);

        CheckRolePermissionRelationVisitor c = new CheckRolePermissionRelationVisitor(permissionDao,relationDao);
        copy.accept(c);

        if(!c.getStatus()){
            DeletePermissionVisitor delete = new DeletePermissionVisitor(permissionDao);
            copy.accept(delete);
        }

        return !c.getStatus();
    }

    /**
     * 根据角色id查询权限
     * @param rid
     * @return
     */
    @Override
    public List<PermissionVo> getPermissionListByRoleId(String rid) {
        return permissionDao.getPermissionListByRoleId(rid);
    }


}
